﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Dict.Blo;
using Dict.DataModel;
using ProtoBuf.Message;

namespace UI
{
    public class PetBreachTab : MonoBehaviour
    {
        public UILabel petBreachLevelBefore;
        public UILabel petBreachLevelAfter;
        public UILabel petTalentName;
        public List<UILabel> petBreachBefore;
        public List<UILabel> petBreachAfter;
        public List<GameObject> pointers;
        public List<GameObject> petBreackCostItemPos;
        public List<GameObject> petBreackCostIconRoot;
        public List<UILabel> petBreackCostItemName;
        public List<UILabel> petBreackCostItemCount;
        public GameObject petBreachBtn;
        public GameObject petLevelUpBtn;
        public int petCurLevel = 0;
        bool petBreackEnough = false;
        bool petBreackCostCoinEnough = false;
        string petGuid;

        public void SetPet(string guid)
        {
            petGuid = guid;
            UpdatePetBreachTab();
        }
        public void UpdatePetBreachTab()
        {
            // particleObj.SetActive(false);
            UIPet pet = StaticData.playerData.GetUIPet(petGuid);
            int nextBreachLevel = pet.breachlevel + 1;
            //int maxBreachLevel = DictPetBreachBlo.GetPetBreachMaxLevel(pet.petId, nextBreachLevel);
            //if (nextBreachLevel > maxBreachLevel)
            //{
            //    nextBreachLevel = maxBreachLevel;
            //}
            TablePetTalentMaterial breachData = DictPetBreachBlo.GetPetBreachLevelData(pet.petId, nextBreachLevel);

            //突破后。获取到的随机天赋
           // TablePetTalent petTalentData = DictPetBreachBlo.GetPetBreachTalent(pet.petId, pet.breachlevel);


            //是否可以突破
            if (pet.level > breachData.petLevel)
            {
                //可以突破
                petBreachBtn.SetActive(true);
                petLevelUpBtn.SetActive(false);
            }
            else
            {
                petBreachBtn.SetActive(false);
                petLevelUpBtn.SetActive(true);
            }

            // clear
            for (int i = 0; i < petBreackCostIconRoot.Count; i++)
            {
                petBreackCostIconRoot[i].transform.DestroyChildren();
                petBreackCostItemCount[i].text = "";
            }

            // 消耗
            string[] costList = breachData.cost.Split(';');
            int costCoin = 0;
            int count = 0;
            petBreackEnough = true;
            for (int i = 0; i < costList.Length; i++)
            {
                string[] costinfo = costList[i].Split(',');
                if (costinfo[0] == "coin")
                {
                    costCoin = int.Parse(costinfo[1]);
                    if (costCoin > StaticData.playerData.coin)
                        petBreackCostCoinEnough = false;
                    else
                        petBreackCostCoinEnough = true;
                }
                else if (costinfo[0].StartsWith(Dict.Config.TableConst.pet))
                {
                    GameObject obj = AssetLoad.ResourceManager.Instance.LoadResource("prefab/PetIcon") as GameObject;
                    GameObject petIcon = Instantiate(obj);
                    petIcon.GetComponent<PetIcon>().SetPet(costinfo[0]);
                    petIcon.transform.parent = petBreackCostIconRoot[count].transform;
                    petIcon.transform.localPosition = new Vector3(0, 0, 0);
                    petIcon.transform.localScale = new Vector3(0.9f, 0.9f, 0.9f);
                    UIWidget[] widgets = petIcon.transform.GetComponentsInChildren<UIWidget>(true);
                    for (int j = 0; j < widgets.Length; j++)
                    {
                        widgets[j].depth = petBreackCostIconRoot[count].GetComponent<UIWidget>().depth + 1 + j;
                    }

                    petBreackCostItemName[count].text = TableTextBlo.GetText("name_" + costinfo[0]).Replace("\\r", "\r").Replace("\\n", "\n");

                    List<UIPet> petList = StaticData.playerData.InstPlayerData.GetPetBreachAvailableList(pet.petId);
                    if (petList.Count < int.Parse(costinfo[1]))
                    {
                        petBreackEnough = false;
                    }
                    petBreackCostItemCount[count].text = petList.Count.ToString() + "/" + costinfo[1];

                    count++;
                }
                else if (costinfo[0].StartsWith(Dict.Config.TableConst.item))
                {
                    long itemCount = StaticData.playerData.GetSimpleItemCount(costinfo[0]);

                    GameObject obj = AssetLoad.ResourceManager.Instance.LoadResource("prefab/ItemIcon") as GameObject;
                    GameObject itemIcon = Instantiate(obj);
                    itemIcon.GetComponent<ItemIcon>().SetItem(costinfo[0]);
                    itemIcon.transform.parent = petBreackCostIconRoot[count].transform;
                    itemIcon.transform.localPosition = new Vector3(0, 0, 0);
                    itemIcon.transform.localScale = new Vector3(0.9f, 0.9f, 0.9f);
                    UIWidget[] widgets = itemIcon.transform.GetComponentsInChildren<UIWidget>(true);
                    for (int j = 0; j < widgets.Length; j++)
                    {
                        widgets[j].depth = petBreackCostIconRoot[count].GetComponent<UIWidget>().depth + 1 + j;
                    }

                    petBreackCostItemCount[count].text = itemCount.ToString() + "/" + costinfo[1];
                    petBreackCostItemName[count].text = TableTextBlo.GetText("name_" + costinfo[0]).Replace("\\r", "\r").Replace("\\n", "\n");

                    int needCount = int.Parse(costinfo[1]);
                    if (itemCount < needCount)
                    {
                        petBreackEnough = false;
                    }

                    count++;
                }
            }

            //多余的位置隐藏
            int w = 440 / (count + 1);
            int x = w - 440 / 2;

            for (int i = 0; i < petBreackCostItemPos.Count; i++)
            {
                if (i >= count)
                {
                    petBreackCostItemPos[i].SetActive(false);
                }
                else
                {
                    Vector3 pos = petBreackCostItemPos[i].transform.localPosition;
                    petBreackCostItemPos[i].transform.localPosition = new Vector3(x + i * w, pos.y, pos.z);
                    petBreackCostItemPos[i].SetActive(true);
                }
            }

            //满级处理
          //  pointers[0].SetActive(true);
            //if (pet.breachLevel >= maxBreachLevel)
            //{
            //    for (int i = 0; i < pointers.Count; i++)
            //    {
            //        pointers[i].SetActive(false);
            //    }
            //}
        }

        public void OnBreachClick()
        {
            CSPetBreached msg = new CSPetBreached();
            msg.guid = petGuid;
            NetAPI.SendHttp(OpDefine.CSPetBreached, msg, BreachCallback);
        }

        public void OnGoToLevelUp()
        {
            Debug.Log("OnGoToLevelUp ");
        }
        void BreachCallback(NetWork.Packets.Packet packet, bool bSuccess)
        {
            if (bSuccess)
            {
                SCPetBreached msg = packet.kBody as SCPetBreached;
                StaticData.playerData.Delete(msg.cost);
                StaticData.playerData.UpdatePet(msg.pet);
                UpdatePetBreachTab();
                AudioManager.Instance.PlayUIAudioChannel("shipinjinglian");
            }
        }
    }

}


